// import mixins from './mixins'
import i18n from "@/lang";
import utilEventBus from "@/utils/eventBus";
// import { checkPermission } from "@/logics/user/permission";
import Clickoutside from "element-ui/src/utils/clickoutside";
import { Message } from "element-ui";
import store from "@/store";
export const ScMessage = {
    success: function (msgKey = "messageBox.success") {
        Message({
            customClass: 'el-message-index',
            type: "success",
            message: i18n.t(msgKey)
        });
    },
    info: function (msgKey) {
        Message({
            customClass: 'el-message-index',
            type: "info",
            message: i18n.t(msgKey)
        });
    },
    warn: function (warnMsg, values) {
        Message({
            customClass: 'el-message-index',
            type: "warning",
            message: i18n.t(warnMsg, values)
        });
    },
    error: function (warnMsg, values) {
        Message({
            customClass: 'el-message-index',
            type: "error",
            message: i18n.t(warnMsg, values)
        });
    }
};
const requireComponent = require.context(
    // 全局组件自动加载注册：在components / 新建global文件夹（放置全局组件）
    "./",
    // 是否查询其子目录
    true,
    // 匹配基础组件文件名的正则表达式
    /\.vue$/
);

export default function (Vue, options) {
    // // 1. 添加全局方法或 property
    // Vue.myGlobalMethod = function () {
    //   // 逻辑...
    // }

    // // 2. 添加全局资源
    Vue.directive("clickoutside", Clickoutside);

    // 3. 注入组件选项
    // Vue.mixin(mixins)

    // 4. 添加实例方法
    // 4.1 全局事件总线
    Vue.prototype.$EventBus = utilEventBus;

    Vue.prototype.$scConfirm = function (title, message, option = {}) {
        const { stopMouseDown, customClass, ...otherOption } = option;
        const confirmResult = this.$confirm(title, message, {
            confirmButtonText: this.$t("messageBox.confirm"),
            cancelButtonText: this.$t("messageBox.cancel"),
            confirmButtonClass: "sc-btn-medium",
            cancelButtonClass: "sc-btn-medium sc-btn-ghost",
            ...otherOption
        });
        //不会同时出现两个确认框。popper中弹出确认框，需要停止事件冒泡，防止关闭Popper
        if (stopMouseDown) {
            this.$nextTick().then(() => {
                const boxElement = document.querySelector(".el-message-box__wrapper");
                boxElement.addEventListener("mousedown", e => {
                    e.stopPropagation();
                });
                boxElement.addEventListener("click", e => {
                    e.stopPropagation();
                });
                boxElement.addEventListener("mouseup", e => {
                    e.stopPropagation();
                });
            });
        }
        return confirmResult;
    };

    Vue.prototype.$scMessage = ScMessage;

    // 权限验证
    // Vue.prototype.$checkPermission = checkPermission;

    //弹框
    Vue.prototype.$openDialog = (key, value) => {
        store.commit("orange/openDialog", { key, value })
    };
    Vue.prototype.$closeDialog = (key) => {
        store.commit("orange/closeDialog", key)
    };
    Vue.prototype.$isOpen = (key) => {
        const isOpen = store.getters["orange/isOpen"];
        return isOpen(key)
    };


    /** 自动加载组件 */
    requireComponent.keys().forEach(fileName => {
        // 获取组件配置
        const componentConfig = requireComponent(fileName);
        // 全局注册组件
        Vue.component(
            // 如果这个组件选项是通过 `export default` 导出的，
            // 那么就会优先使用 `.default`，
            // 否则回退到使用模块的根。
            componentConfig.default.name || componentConfig.name,
            componentConfig.default || componentConfig
        );
    });
}
